package com.exchange.code;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.exchange.aspect.VisitLimit;
import com.exchange.aspect.logger.SysLogger;
import com.exchange.aspect.user.CheckLogin;
import com.exchange.code.dto.AddCodeTypeDto;
import com.exchange.code.dto.SelCodeTypeDto;
import com.exchange.code.entity.SysCodeType;
import com.exchange.group.SelectGroup;
import com.exchange.code.service.SysCodeService;
import com.exchange.utils.result.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 *                    .::::.
 *                  .::::::::.
 *                 :::::::::::
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 *
 * @Author liuhai
 * @Date 2020-11-12 09:32:37
 * @Description 代码管理
 */
@RestController
@RefreshScope
@RequestMapping("/system/code")
@Api(value = "代码管理", tags = "代码管理接口")
public class CodeController {

    @Autowired
    private SysCodeService sysCodeService;

    @PostMapping("/addCodeType")
    @ApiOperation("添加代码分类")
    @SysLogger(desc = "添加代码分类", type = "code-01")
    @CheckLogin
    @VisitLimit(count = 1, timeout = 5)
    public CommonResult userLogin(@RequestBody @Validated(value = SelectGroup.class) AddCodeTypeDto codeTypeDto){
        if(null == codeTypeDto || StringUtils.isEmpty(codeTypeDto.getCodeTypeName())){
            return CommonResult.fail("参数不能为空，代码分类名称必填");
        }
        return sysCodeService.addCodeType(codeTypeDto);
    }

    @PostMapping("/getCodeTypeList")
    @ApiOperation("查询代码分类列表")
    @SysLogger(desc = "查询代码分类列表", type = "code-02")
    public CommonResult getUserList(@RequestBody @Validated(value = SelectGroup.class) SelCodeTypeDto codeTypeDto){
        IPage<SysCodeType> codeTypeIPage = sysCodeService.getCodeTypeList(codeTypeDto);
        if(null == codeTypeIPage){
            return CommonResult.fail("获取代码分类列表失败");
        }
        return CommonResult.successWithData("获取代码分类列表成功", codeTypeIPage);
    }



}
